<template>
  <el-dialog
    title="批量发布"
    :visible.sync="visible"
    width="60%"
    :before-close="handleClose"
    custom-class="batch-publish-dialog">
    <div class="batch-publish-container">
      <div class="header-section">
        <div class="title-section">
          <i class="el-icon-document"></i>
          <span>课程信息</span>
        </div>
        <div class="course-info">
          <div class="info-item">
            <span class="label">课程名称：</span>
            <span class="value">{{courseName}}</span>
          </div>
          <div class="info-item">
            <span class="label">班级：</span>
            <span class="value">{{banji}}</span>
          </div>
          <div class="info-item">
            <span class="label">学期：</span>
            <span class="value">{{xueqi}}</span>
          </div>
          <div class="info-item">
            <span class="label">课程类型：</span>
            <span class="value">{{kechengleixing}}</span>
          </div>
        </div>
      </div>

      <div class="button-group">
        <el-button type="primary" icon="el-icon-download" @click="getTemplate" :loading="templateLoading">获取Excel模板</el-button>
        <el-upload
          class="upload-demo"
          action="#"
          :auto-upload="false"
          :on-change="handleFileChange"
          :before-upload="beforeUpload"
          :show-file-list="false"
          accept=".xlsx,.xls">
          <el-button type="success" icon="el-icon-upload2" :loading="uploadLoading">上传Excel文件</el-button>
        </el-upload>
      </div>

      <div class="table-container" v-if="tableData.length > 0">
        <div class="title-section">
          <i class="el-icon-tickets"></i>
          <span>上传数据预览</span>
        </div>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          :header-cell-style="{
            background: '#f5f7fa',
            color: '#606266',
            fontWeight: 'bold'
          }"
          :cell-style="{
            padding: '8px 0'
          }">
          <el-table-column
            prop="id"
            label="序号"
            width="80"
            align="center">
          </el-table-column>
          <el-table-column
            prop="ketimingcheng"
            label="课题名称"
            width="180"
            align="center">
          </el-table-column>
          <el-table-column
            prop="zuoyeyaoqiu"
            label="作业要求"
            align="center">
          </el-table-column>
          <el-table-column
            prop="status"
            label="状态"
            width="100"
            align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === '已发布' ? 'success' : 'info'" size="medium">
                {{scope.row.status}}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      courseId: null,
      courseName: '',
      banji: '',
      xueqi: '',
      kechengleixing: '',
      templateLoading: false,
      uploadLoading: false,
      tableData: []
    }
  },
  methods: {
    init(id, name, banji, xueqi, kechengleixing) {
      this.courseId = id
      this.courseName = name
      this.banji = banji || '暂无数据'
      this.xueqi = xueqi || '暂无数据'
      this.kechengleixing = kechengleixing || '暂无数据'
      this.visible = true
      this.tableData = []
    },
    handleClose() {
      this.visible = false
      this.tableData = []
    },
    handleConfirm() {
      // 暂时不实现具体功能
      this.handleClose()
    },
    getTemplate() {
      this.templateLoading = true
      this.$http({
        url: 'kechengxinxi2/get',
        method: 'get',
        responseType: 'blob'
      }).then(response => {
        const blob = new Blob([response.data], { 
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
        })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = '模板文件.xlsx'
        link.click()
        window.URL.revokeObjectURL(link.href)
        this.$message.success('模板下载成功')
      }).catch(error => {
        this.$message.error('模板下载失败')
        console.error('下载模板失败:', error)
      }).finally(() => {
        this.templateLoading = false
      })
    },
    beforeUpload(file) {
      const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || 
                      file.type === 'application/vnd.ms-excel'
      if (!isExcel) {
        this.$message.error('只能上传Excel文件!')
        return false
      }
      return true
    },
    handleFileChange(file) {
      if (!this.courseId) {
        this.$message.error('课程ID不能为空')
        return
      }

      this.uploadLoading = true
      const formData = new FormData()
      formData.append('file', file.raw)
      
      this.$http({
        url: `kechengxinxi2/put/${this.courseId}`,
        method: 'post',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.tableData = data.data || []
          this.$message.success(`成功上传${this.tableData.length}条数据`)
        } else {
          this.$message.error(data.msg || '文件上传失败')
        }
      }).catch(error => {
        this.$message.error('文件上传失败')
        console.error('上传失败:', error)
      }).finally(() => {
        this.uploadLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.batch-publish-dialog {
  .el-dialog__header {
    padding: 20px;
    border-bottom: 1px solid #ebeef5;
  }
  
  .el-dialog__body {
    padding: 0;
  }
  
  .el-dialog__footer {
    padding: 20px;
    border-top: 1px solid #ebeef5;
  }
}

.batch-publish-container {
  min-height: 200px;
  
  .header-section {
    padding: 20px;
    background-color: #f8f9fa;
    border-bottom: 1px solid #ebeef5;
    
    .title-section {
      display: flex;
      align-items: center;
      margin-bottom: 15px;
      
      i {
        font-size: 18px;
        color: #409EFF;
        margin-right: 8px;
      }
      
      span {
        font-size: 16px;
        font-weight: bold;
        color: #303133;
      }
    }
    
    .course-info {
      padding-left: 26px;
      
      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 12px;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .label {
          color: #606266;
          margin-right: 8px;
          min-width: 80px;
        }
        
        .value {
          color: #303133;
          font-weight: 500;
        }
      }
    }
  }
  
  .button-group {
    display: flex;
    gap: 20px;
    align-items: center;
    padding: 20px;
    background-color: #fff;
    border-bottom: 1px solid #ebeef5;
    
    .upload-demo {
      margin-left: 10px;
    }
  }

  .table-container {
    padding: 20px;
    
    .title-section {
      display: flex;
      align-items: center;
      margin-bottom: 15px;
      
      i {
        font-size: 18px;
        color: #409EFF;
        margin-right: 8px;
      }
      
      span {
        font-size: 16px;
        font-weight: bold;
        color: #303133;
      }
    }
  }
}

.el-tag {
  padding: 0 12px;
  height: 28px;
  line-height: 26px;
  border-radius: 4px;
}
</style> 